#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    InitUI();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::InitUI()
{
    QFont font;
    this->resize((ROW+7)*xsize,(COL+1)*ysize);//绘制画板大小
    this->setFixedSize((ROW+7)*xsize,(COL+1)*ysize);//固定了画板大小
    this->setWindowTitle("Qtchess");

    font.setPointSize(20);
    button = new QPushButton("悔棋",this);
    button->setFont(font);
    button->move((ROW+1)*xsize,13.5*ysize);
    button->resize(5.5*xsize,2.0*ysize);
    connect(button,SIGNAL(clicked(void)),this,SLOT(chessback()));

    label = new QLabel("五子棋游戏",this);
    label->move((ROW+2)*xsize,6*ysize);
    label->setFont(font);
    label->resize(40*80,50);

    label_2 = new QLabel("默认黑棋先手",this);
    label_2->move((ROW+2)*xsize,8*ysize);
    label_2->setFont(font);
    label_2->resize(40*100,50);

    handchess_color = false; // false -> 黑色 true -> 白色
}


void MainWindow::paintEvent(QPaintEvent *)
{
    drawchessboard();
    drawhandchess();
    drawchessitem();
    update();//会触发paintEvent事件
}

void MainWindow::drawchessboard()
{
    QPainter painter(this);//创建画家对象
    painter.setPen(QPen(QColor(Qt::black),4));//给边框线颜色和大小
    painter.setBrush(Qt::gray);//给其他格子颜色
    for(int i = 0;i < COL;i++)
      for(int j = 0;j < ROW;j++)
           painter.drawRect((0.5+i)*xsize,(0.5+j)*ysize,xsize,ysize);//绘制棋盘
}

void MainWindow::drawhandchess()
{
    QPainter painter(this);
    painter.setPen(QPen(QColor(Qt::transparent)));//给棋子边框设置透明
    if(handchess_color == true)
        painter.setBrush(Qt::white);
    else
        painter.setBrush(Qt::black);
    //获得鼠标单击点的全局位置，长度，宽度
    painter.drawEllipse(mapFromGlobal(QCursor::pos()),xsize/2,ysize/2);
}
//在棋盘上形成棋子
void MainWindow::drawchessitem()
{
    QPainter painter(this);
    painter.setPen(QPen(QColor(Qt::transparent)));
    for(int i = 0;i < p_chessItem.size();i++)
    {
        ChessItem item = p_chessItem[i];
        if(item._color == true)
        {
            painter.setBrush(Qt::white);
        }
        else
        {
            painter.setBrush(Qt::black);
        }
        drawchessplace(painter,item._pt);
    }
}

void MainWindow::drawchessplace(QPainter &painter, QPoint &point)
{
    QPoint ptCenter((point.x() + 0.5) * xsize,(point.y() + 0.5) * ysize);
    painter.drawEllipse(ptCenter,xsize/2,ysize/2);
}

void MainWindow::mousePressEvent(QMouseEvent *event)
{
    QPoint pt;
    if(event->pos().x()/xsize >= 16)
        return;
    pt.setX(event->pos().x()/xsize);
    pt.setY(event->pos().y()/ysize);
    qDebug()<<"x:"<<event->pos().x()/xsize<<"y:"<<event->pos().y()/ysize<<' ';
    for(int i = 0;i <p_chessItem.size();i++)
    {
        ChessItem item = p_chessItem[i];
        if(item._pt == pt)
        return;
    }

    ChessItem item(pt,handchess_color);
    p_chessItem.append(item);//添加元素

    int left = checkchess(item,QPoint(-1,0));
    int right = checkchess(item,QPoint(1,0));
    int leftup = checkchess(item,QPoint(-1,-1));
    int rightup = checkchess(item,QPoint(1,-1));
    int leftdown = checkchess(item,QPoint(-1,1));
    int rightdown = checkchess(item,QPoint(1,1));
    int up = checkchess(item,QPoint(0,-1));
    int down = checkchess(item,QPoint(0,1));

    if((left + right)>= 4 || (up + down)>=4 ||(leftup+rightdown)>=4 || (leftdown+rightup)>=4)
    {
        QString str = handchess_color ?"White victory!" : "Black victory";
        QMessageBox::information(NULL,"Game Over",str,QMessageBox::Yes);
        p_chessItem.clear();
    }
    handchess_color = !handchess_color;
}

int MainWindow::checkchess(ChessItem item,QPoint pt)
{
    int count = 0;
    item._pt += pt;
    //contains() -> This function requires the value type to have an implementation of operator==().
    while(p_chessItem.contains(item))
    {
        count++;
        item._pt+=pt;
    }
    return count;
}
//悔棋功能:
void MainWindow::chessback()
{
   if(!p_chessItem.empty())
   {
       QPainter painter(this);
       ChessItem item = p_chessItem[p_chessItem.size()-1];
       qDebug() << item._pt << ' ';
       p_chessItem.pop_back();
   }
   else
       qDebug()<<"empty";
}
